
package com.winning.sx.microframework.controller.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.common.util.Constains;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.base.EquipmentService;
import com.winning.sx.microframework.service.sysmanage.ConstService;
import com.winning.sx.microframework.service.sysmanage.RoleManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/base/warehouse")
public class WarehouseController {
    @Autowired
    EasyService easyService;
    @Autowired
    RoleManageService service;
    @Autowired
    EquipmentService equipmentService;
    @Autowired
    ConstService services;

    @RequestMapping(value = {"/", ""})
    public ModelAndView init() throws IOException, CustomException, SQLException {
        ModelAndView mv = new ModelAndView("/base/warehouse");
        List state = services.queryCount("200");
        mv.addObject("state", state);
        Map organization = new HashMap();
        organization.put("superOrganizationId", "0");
        organization.put("organizationType", "400001");
        DataTable organizationEntity = easyService.queryList("BASE_Organization", organization);
        List organizationName = organizationEntity.getDataRows();
        mv.addObject("organizationName", organizationName);
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryList"})
    public AjaxTableResponseMessage queryList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            if (param.get("organization") == null || "0".equals(param.get("organization").toString())) {
                throw new Exception("请选择机构");
            } else {
                param.put("superOrganizationId", param.get("organization"));
                param.put("organizationType", "400001");
            }
            DataTable all = easyService.querySql("select w.id,w.organizationId,o.name orgName, w.name,w.code,w.position,w.createTime,w.updateTime,w.state," +
                    " (select count(id) from BASE_WarehouseStaff s where w.id=s.warehouseId and s.validFlag='100001' ) " +
                    " managerCount," +
                    " (select count(id) from BASE_WarehouseType s where w.id=s.warehouseId and s.validFlag='100001') " +
                    " eqCount from BASE_Warehouse w left join BASE_Organization o on w.organizationId=o.id " +
                    " where w.validFlag='100001' and w.organizationId in ("
                    +services.queryMyOrg(param).get("allorgs")+
                    ") group by w.id,w.organizationId,o.name, w.name,w.createTime,w.updateTime,w.code,w.position,w.state", param)
            ;
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/save"})
    public AjaxDataResponseMessage insert(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                if (map.get("organizationId") == null || "0".equals(map.get("organizationId").toString())) {
                    throw new Exception("请选择机构");
                }
                Map param = new HashMap();
                param.put("id", map.get("organizationId"));
                Map org = easyService.queryEntity("BASE_Organization", param);
                if (!Constains.WAREHOUSEFLAG_YES.equals(org.get("warehouseFlag").toString())) {
                    throw new Exception("此机构不允许创建仓库，请选择其他机构或联系管理员开通创建仓库权限");
                }
                map.put("state", "200001");
                easyService.save("BASE_warehouse", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/delete"})
    public AjaxDataResponseMessage delete(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("BASE_warehouse", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("删除失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryManagerList"})
    public AjaxTableResponseMessage queryManagerList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            if (param.get("warehouseId") == null || "0".equals(param.get("warehouseId").toString())) {
                throw new Exception("请选择仓库");
            }
            DataTable all = easyService.querySql("select s.*,f.name staffName,f.telephone,f.job from BASE_WarehouseStaff s left join BASE_Staff f on s.staffId=f.id where s.warehouseId=:warehouseId and s.validFlag='100001' ", param);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/queryEqClassList"})
    public AjaxTableResponseMessage queryEqClassList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            if (param.get("warehouseId") == null || "0".equals(param.get("warehouseId").toString())) {
                throw new Exception("请选择仓库");
            }
            DataTable all = easyService.querySql("select s.*,q.name typeName  from BASE_WarehouseType s left join BASE_EqClass q on s.oneCode=q.code where s.warehouseId=:warehouseId and s.validFlag='100001' ", param);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/saveStaff"})
    public AjaxDataResponseMessage saveStaff(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                map.put("state", "200001");
                easyService.save("BASE_warehouseStaff", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/saveType"})
    public AjaxDataResponseMessage saveType(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                map.put("state", "200001");
                easyService.save("BASE_warehouseType", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/deleteStaff"})
    public AjaxDataResponseMessage deleteStaff(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("BASE_warehouseStaff", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("删除失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/deleteType"})
    public AjaxDataResponseMessage deleteType(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                easyService.delete("BASE_warehouseType", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("删除失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }
}

